import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:umeng_push_demo/controller/app_controller.dart';
import 'package:umeng_push_demo/model/push_manager.dart';
import 'package:umeng_push_demo/routes.dart';
import 'package:umeng_push_demo/view/components/setting_input_item.dart';
import 'package:get/get.dart';

/// FileName setting_page
///
/// @Author 陈晓星
/// @Date 2024/10/28 08:41
///
/// @Description TODO

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  late TextEditingController tagController = TextEditingController();
  late FocusNode tagFocusNode = FocusNode();
  late TextEditingController aliasController = TextEditingController();
  late FocusNode aliasFocusNode = FocusNode();

  @override
  initState() {
    super.initState();
    final appController = Get.put(AppController());
    tagController.text = appController.tag.value;
    aliasController.text = appController.alias.value;
  }

  /// 保存
  Future<void> saveAction() async {
    bool isSave = await showDialog(context: context, builder: (context) {
      return AlertDialog(
        title: const Text('提醒'),
        content: const Text('是否确认保存？'),
        actions: <Widget>[
          TextButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: const Text('取消')
          ),
          TextButton(
              onPressed: () => Navigator.of(context).pop(true),
              child: const Text('保存')
          ),
        ],
      );
    });
    if (!isSave) return;

    await PushManager.setTag(tagController.text);
    await PushManager.setAlias(aliasController.text);
    Fluttertoast.showToast(msg: '保存成功');
    AppRoutes.back();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("设置"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(height: 15.w,),
            SettingInputItem(
              name: '标签',
              textEditingController: tagController,
              focusNode: tagFocusNode
            ),
            SettingInputItem(
              name: '别名',
              textEditingController: aliasController,
              focusNode: aliasFocusNode
            ),
            SizedBox(height: 20.w,),
            ElevatedButton(
              onPressed: saveAction,
              child: const Padding(
                padding: EdgeInsets.symmetric(vertical: 0, horizontal: 60),
                child: Text('保存'),
              )
            )
          ],
        ),
      )
    );
  }
}
